//
//  MLProgressHUD.m
//  CustomerPo
//
//  Created by molon on 14/12/17.
//  Copyright (c) 2014年 molon. All rights reserved.
//

#import "MLProgressHUD.h"
#import "MLKitDebug.h"

@implementation MLProgressHUD

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [super willMoveToSuperview:newSuperview];
    
    if ([self.superview isKindOfClass:[UIScrollView class]]) {
        //删除KVO
        [self.superview removeObserver:self forKeyPath:@"contentOffset" context:nil];
    }
}

- (void)didMoveToSuperview
{
    [super didMoveToSuperview];
    
    if ([self.superview isKindOfClass:[UIScrollView class]]) {
        //添加KVO
        [self.superview addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil];
    }
}

#pragma mark - kvo
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([@"contentOffset" isEqualToString:keyPath]) {
        UIScrollView *scrollView = (UIScrollView*)self.superview;
        
        //调整这玩意的位置
        CGPoint contentOffset = scrollView.contentOffset;
        //这个位置+半个frame的宽度
        self.center = CGPointMake(contentOffset.x+scrollView.frame.size.width/2, contentOffset.y+scrollView.frame.size.height/2+self.yOffset);
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}


#pragma mark - helper
+ (void)showTips:(NSString*)tips withUserInteractionEnabled:(BOOL)userInteractionEnabled andView:(UIView*)view
{
    [self showTips:tips withUserInteractionEnabled:userInteractionEnabled andView:view andYOffset:-50.0f];
}

+ (void)showTips:(NSString*)tips withUserInteractionEnabled:(BOOL)userInteractionEnabled andView:(UIView*)view andYOffset:(CGFloat)yOffset
{
    [self showTips:tips withUserInteractionEnabled:userInteractionEnabled andView:view andYOffset:yOffset andDelay:1.5f];
}

+ (void)showTips:(NSString*)tips withUserInteractionEnabled:(BOOL)userInteractionEnabled andView:(UIView*)view andYOffset:(CGFloat)yOffset andDelay:(NSTimeInterval)delay
{
    if (!view) {
        DLOG(@"传递进来的View是空的");
        return;
    }
    
    MLProgressHUD *hud = [[self class] showHUDAddedTo:view animated:YES];
    hud.userInteractionEnabled = userInteractionEnabled;
    if ([view isKindOfClass:[UIScrollView class]]) {
        ((UIScrollView*)view).scrollEnabled = !hud.userInteractionEnabled;
    }
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabelText = tips;
    hud.detailsLabelFont = [UIFont boldSystemFontOfSize:14.0f];
    hud.removeFromSuperViewOnHide = YES;
    hud.margin = 10.f;
    hud.yOffset = yOffset;
    [hud hide:YES afterDelay:delay];
}

+ (MB_INSTANCETYPE)showHUDAddedToScrollView:(UIScrollView *)scrollView animated:(BOOL)animated
{
    scrollView.scrollEnabled = NO;
    MLProgressHUD *hud = [self HUDForView:scrollView];
    if (hud != nil) {
        return hud;
    }
    
    hud = [[[self class] alloc] initWithView:scrollView];
    [scrollView addSubview:hud];
    [hud show:animated];
    
    return hud;
}

+ (BOOL)hideHUDForScrollView:(UIScrollView *)scrollView animated:(BOOL)animated
{
    scrollView.scrollEnabled = YES;
    return [super hideHUDForView:scrollView animated:animated];
}

+ (BOOL)hideAllHUDsForScrollView:(UIScrollView *)scrollView animated:(BOOL)animated
{
    scrollView.scrollEnabled = YES;
    return [super hideAllHUDsForView:scrollView animated:animated];
}


+ (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view animated:(BOOL)animated
{
    if (!view) {return nil;}
    if ([view isKindOfClass:[UIScrollView class]]) {
        return [[self class]showHUDAddedToScrollView:(UIScrollView*)view animated:animated];
    }
    return [super showHUDAddedTo:view animated:animated];
}

+ (NSUInteger)hideAllHUDsForView:(UIView *)view animated:(BOOL)animated
{
    if (!view) {return NO;}
    if ([view isKindOfClass:[UIScrollView class]]) {
        return [[self class]hideAllHUDsForScrollView:(UIScrollView*)view animated:animated];
    }
    return [super hideAllHUDsForView:view animated:animated];
}

+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated
{
    if (!view) {return NO;}
    if ([view isKindOfClass:[UIScrollView class]]) {
        return [[self class]hideHUDForScrollView:(UIScrollView*)view animated:animated];
    }
    return [super hideHUDForView:view animated:animated];
}

@end
